package com.atguigu.tingshu.user.login;/*
 * title:
 * author:
 * date:
 * description:
 * */

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Component
public class LoginClient implements ApplicationContextAware {
    private static final Map<String,ILogin> map=new ConcurrentHashMap<>();
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, Object> beanMap = applicationContext.getBeansWithAnnotation(BeanType.class);
        if (CollectionUtils.isEmpty(beanMap)){
            return;
        }
        Collection<Object> beanList = beanMap.values();
        if (CollectionUtils.isEmpty(beanList)){
            return;
        }
        beanList.forEach(bean->{
            BeanType annotation = bean.getClass().getAnnotation(BeanType.class);
            map.put(annotation.value().type,(ILogin) bean);
        });
    }

    public Map<String,Object> login(String type,Map<String,Object> paramMap){
       return map.get(type).login(paramMap);
    }
}
